﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class UIGSC : ViewBase
    {
        public VGSCOLOR TMV
        {
            get
            {
                return (VGSCOLOR)MV;
            }
        }
        #region ==CART===========
        public static readonly DependencyProperty CARTProperty = DependencyProperty.Register("CART",
            typeof(string), typeof(UIGSC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCARTChanged));
        public string CART
        {
            get
            {
                return (string)GetValue(CARTProperty);
            }
            set
            {
                SetValue(CARTProperty, value);
                OnCARTChg();
            }
        }
        private static void OnCARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSC;
            if (src != null)
            {
                src.CART = (string)e.NewValue;
                
            }
        }
        #endregion

        #region ==IsAutoGSC===========
        public static readonly DependencyProperty IsAutoGSCProperty = DependencyProperty.Register("IsAutoGSC",
            typeof(bool), typeof(UIGSC),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsAutoGSCChanged));
        public bool IsAutoGSC
        {
            get
            {
                return (bool)GetValue(IsAutoGSCProperty);
            }
            set
            {
                SetValue(IsAutoGSCProperty, value);
            }
        }
        private static void OnIsAutoGSCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSC;
            if (src != null)
            {
                src.IsAutoGSC = (bool)e.NewValue;
            }
        }
        #endregion

        public UIGSC()
        {
    
        }

        public virtual void OnCARTChg()
        {
            TBMain.Clear();
            if (CART.Length < 33)
                return;
            DataRow dwData = TBMain.NewRow();
            dwData["GSCMANDANT"] = app.Mandant;
            dwData["GSCART"] = Helpers.Helper.Left(CART, 20);
            dwData["GSCVAR"] = CART.Substring(20, 3);
            dwData["GSCFARBE"] = Helper.Right(CART, 10); 
            DataSet SResult = ((VGSCOLOR)MV).QueryGSCOLOR(dwData, 1);
            MergeData(SResult); 
        }
        public void Insert(DataRow _dwGS)
        {
            _dwGS["GSCNEUBEN"] = app.User;
            _dwGS["GSCNEUDAT"] = MV.date;

            TMV.InsertGSCOLOR(_dwGS, app.company);
            TBMain.Rows.InsertAt(_dwGS, 0);
            vwMaster.View.MoveCurrentToFirst();
        } 
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster != null)
            {
                KEY = "G"+TMV.GSCART+TMV.GSCVAR+TMV.GSCFARBE;

            }
            else
            {
                KEY = "";
            }
        }
    }
}
